package com.bipa.controller;

import com.bipa.service.ArticleService;
import com.bipa.service.ArticleTypeService;
import com.bipa.util.PagerModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {

    @Autowired
    private ArticleTypeService articleTypeService;

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/index")
    public ModelAndView findTypes(@RequestParam(value = "typeCode",defaultValue = "") String typeCode,@RequestParam(value = "keyword",defaultValue = "") String keyword,@RequestParam(value = "pageIndex",defaultValue = "1")String pageIndex){
        //System.out.println(typeCode.length()+" "+keyword.length());
        ModelAndView mv = new ModelAndView();
        mv.addObject("firstTypes",articleTypeService.findFirstTypes());
        mv.addObject("keyword",keyword);
        mv.addObject("highLight","articleby");
        if (!"".equals(typeCode)){
            mv.addObject("firstCode",typeCode.substring(0,4));
            mv.addObject("secodeTypes",articleTypeService.findSecondTypes(typeCode.substring(0,4)));
        }
        //创建分页实体    用于封装分页相关的信息
        PagerModel pageModel = new PagerModel();
        //获取页码
        if(!pageIndex.equals("")) {
            pageModel.setPageIndex(Integer.valueOf(pageIndex));
        }
        //将总记录数封装至pageModel对象中
        pageModel.setTotalNum(articleService.getTotalNum(typeCode,keyword));
        mv.addObject("articles",articleService.findArticles(typeCode,keyword,pageModel));
        mv.addObject("pageModel",pageModel);
        mv.setViewName("front/main");
        return mv;
    }

    @RequestMapping("/mindex")
    public ModelAndView toMindex(@RequestParam(value = "typeCode",defaultValue = "") String typeCode,@RequestParam(value = "keyword",defaultValue = "") String keyword,@RequestParam(value = "pageIndex",defaultValue = "1")String pageIndex){
        ModelAndView mv = new ModelAndView();
        mv.addObject("firstTypes",articleTypeService.findFirstTypes());
        mv.addObject("keyword",keyword);
        System.out.println(keyword);
        mv.addObject("highLight","articleby");
        if (!"".equals(typeCode)){
            mv.addObject("secodeTypes",articleTypeService.findSecondTypes(typeCode.substring(0,4)));
        }
        //创建分页实体    用于封装分页相关的信息
        PagerModel pageModel = new PagerModel();
        //获取页码
        pageModel.setPageIndex(Integer.valueOf(pageIndex));
        //将总记录数封装至pageModel对象中
        pageModel.setTotalNum(articleService.getTotalNum(typeCode,keyword));
        mv.addObject("articles",articleService.findArticles1(typeCode,keyword,pageModel));
        mv.addObject("pageModel",pageModel);
        mv.setViewName("back/article_list");
        return mv;
    }
}
